<?php
//exit();
$strDate = $_SERVER[argv][1];
set_time_limit(0);
$_Conn = "_ConnMaster";

if($strDate == "" || $strDate == null){
	$arrD = explode("-", date("Y-m-d"));
	$strDate = date("Y-m-d", mktime(0, 0, 0, $arrD[1] - 2, 1, $arrD[0]));
}
//exit("Date ".$strDate);
$_isDBExist = "no";
$_DBSite = "Master";
include("AllWeb.inc.php");

if (!defined('__Modules_Path')) define('__Modules_Path', '/home/share/www/SourceSite-utf8/secure/modules');
foreach($arrWeb as $strWeb){
	switch($strWeb){
		case "luckyball.cc":
		case "3d.usbet33.us":
			$isContinue = true;
			break;

		default:
			$isContinue = false;
			break;
	}
	if($isContinue)continue;

	$_blDBName = "";
	$_blDBHostM = "";
	$_ConnMaster = null;

	print "\n\n".$strWeb."<br />\n==============================================<br />\n";
	$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/".$strWeb."/secure";
	include($_SERVER["DOCUMENT_ROOT"]."/db.inc.php");
	if($_strLogErr != ""){
		print $_strLogErr."<br />\n\n";
		continue;
	}
	if($_blDBName == "")continue;



	//刪除 Log
	$strSQL = "delete FROM `bet_LogRecord` WHERE `ActionDate` < '".$strDate."'";
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		print "LogRecord Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
	}else{
		print "LogRecord OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
	}
	$intT = time();
	$strSQL = "OPTIMIZE TABLE `bet_LogRecord`";
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		print "OPTIMIZE LogRecord Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
	}else{
		print "OPTIMIZE LogRecord OK(".(time() - $intT).")<br />\n";
	}

	//刪除 LoginLog
	$strSQL = "delete from `bet_LoginLog` where AddDate < '".$strDate."'";
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		print "LoginLog Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
	}else{
		print "LoginLog OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
	}
	$intT = time();
	$strSQL = "OPTIMIZE TABLE `bet_LoginLog`";
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		print "OPTIMIZE LoginLog Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
	}else{
		print "OPTIMIZE LoginLog OK(".(time() - $intT).")<br />\n";
	}

	//刪除 Odds 
	$strSQL = "delete from `bet_Odds` where AddDate < '".$strDate."'";
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		print "Odds Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
	}else{
		print "Odds OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
	}
	$intT = time();
	$strSQL = "OPTIMIZE TABLE `bet_Odds`";
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		print "OPTIMIZE Odds Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
	}else{
		print "OPTIMIZE Odds OK(".(time() - $intT).")<br />\n";
	}
	//continue;

	//刪除停用會員資料
	$arrUID = array();
	$strSQL = "select ID from `bet_Member` where isLock = '1' and LockTime < '".$strDate." 00:00:00'";
	if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
		print "Member Find Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
	}else{
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrUID[] = $Rows -> ID;
		}
		if(sizeof($arrUID) > 0){
			$strUID = join(",", $arrUID);
			$strSQL = "delete from `bet_Member` where ID in (".$strUID.")";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				print "Member Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
			}else{
				print "Member OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
			}
			$intT = time();
			$strSQL = "OPTIMIZE TABLE `bet_Member`";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				print "OPTIMIZE Member Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
			}else{
				print "OPTIMIZE Member OK(".(time() - $intT).")<br />\n";
			}

			//刪除限額資料
			$strSQL = "delete from `bet_MemberLimit` where MemID in (".$strUID.")";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				print "MemberLimit Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
			}else{
				print "MemberLimit OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
			}
			$intT = time();
			$strSQL = "OPTIMIZE TABLE `bet_MemberLimit`";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				print "OPTIMIZE MemberLimit Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
			}else{
				print "OPTIMIZE MemberLimit OK(".(time() - $intT).")<br />\n";
			}

			//刪除退水資料
			$strSQL = "delete from `bet_Commission` where MemID in (".$strUID.")";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				print "Commission Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
			}else{
				print "Commission OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
			}
			$intT = time();
			$strSQL = "OPTIMIZE TABLE `bet_Commission`";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				print "OPTIMIZE Commission Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
			}else{
				print "OPTIMIZE Commission OK(".(time() - $intT).")<br />\n";
			}

			//刪除成數記錄資料
			$strSQL = "delete from `bet_RateChangeRecord` where UserID in (".$strUID.")";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				print "RateChangeLog Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
			}else{
				print "RateChangeLog OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
			}
			$intT = time();
			$strSQL = "OPTIMIZE TABLE `bet_RateChangeRecord`";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				print "OPTIMIZE RateChangeLog Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
			}else{
				print "OPTIMIZE RateChangeLog OK(".(time() - $intT).")<br />\n";
			}
		}else{
			print "No Lock Member Data<br />\n";
		}
	}

	//刪除注單資料
	$strSQL = "select MAX(ID) as ID from `bet_BetList` where PayupDate < '".$strDate."'";
	if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
		print "BetList Find Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
	}else{
		while($Rows = PMA_mysql_fetch_object($RS)){
			$intBID = $Rows -> ID;
		}
		if(empty($intBID))$intBID = 0;

		//刪除過關資料
		$strSQL = "delete from `bet_ParlaysNumber` where BetListID <= ".$intBID;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			print "ParlaysNumber Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
		}else{
			print "ParlaysNumber OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
		}
		$intT = time();
		$strSQL = "OPTIMIZE TABLE `bet_ParlaysNumber`";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			print "OPTIMIZE ParlaysNumber Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
		}else{
			print "OPTIMIZE ParlaysNumber OK(".(time() - $intT).")<br />\n";
		}

		//刪除連碼資料
		$strSQL = "delete from `bet_ParlaysNumberEven` where BetListID <= ".$intBID;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			print "ParlaysNumberEven Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
		}else{
			print "ParlaysNumberEven OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
		}
		$intT = time();
		$strSQL = "OPTIMIZE TABLE `bet_ParlaysNumberEven`";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			print "OPTIMIZE ParlaysNumberEven Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
		}else{
			print "OPTIMIZE ParlaysNumberEven OK(".(time() - $intT).")<br />\n";
		}

		//刪除注單資料
		$strSQL = "delete from `bet_BetList` where ID <= ".$intBID;
		print $strSQL;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			print "BetList Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
		}else{
			print "BetList OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
		}
		$intT = time();
		$strSQL = "OPTIMIZE TABLE `bet_BetList`";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			print "OPTIMIZE BetList Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
		}else{
			print "OPTIMIZE BetList OK(".(time() - $intT).")<br />\n";
		}

		$strSQL = "delete from `bet_BetListContent` where ID <= ".$intBID;
		print $strSQL;
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			print "BetListContent Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
		}else{
			print "BetListContent OK(".mysql_affected_rows($_ConnMaster).")<br />\n";
		}
		$intT = time();
		$strSQL = "OPTIMIZE TABLE `bet_BetListContent`";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			print "OPTIMIZE BetListContent Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
		}else{
			print "OPTIMIZE BetListContent OK(".(time() - $intT).")<br />\n";
		}
	}
}

print "\nEnd";
?>